/*
 * $Id$
 */
package py.windmill.resolution;

import py.windmill.action.MimeType;
import py.windmill.action.Request;
import py.windmill.util.JsonObject_;

/**
 *
 * @author myang
 */
public class JSON extends ResolutionSupport {

    public JSON() {
        super(MimeType.JSON.attr("charset", "utf-8"));
    }

    @Override
    public void execute(final Request req, final Object val) {
        req.response().println(JsonObject_.toString(val));
    }

    @Override
    public void onException(final Request req, final Exception ex) {
        req.response().println(JsonObject_.toString(ex.getMessage()));
    }

    @Override
    public boolean isAcceptedBy(final Request req) {
        return "XMLHttpRequest".equals(req.header("X-Requested-With"))
          || super.isAcceptedBy(req);
    }
}
